/*
 ============================================================================
 Name        : statistics.c
 Author      : Alvaro Pereda
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "include/statistics.h"

int main() {
	statistics();
	return 0;
}

float avg(float *array) {
	int i;
	float result = 0;

	for(i = 0; i < ARRAY_SIZE; i++) {
		result += array[i];
	}

	return result / (float)ARRAY_SIZE;
}

float var(float *array) {
	int i;
	float average;
	float result = 0;
	float tmp;

	average = avg(array);

	for (i = 0; i < ARRAY_SIZE; i++) {
		tmp = array[i] - average;
		result += tmp * tmp;
	}

	return result / (float)ARRAY_SIZE;
}

float* build() {
	int i;
	float *array;

	array = malloc(ARRAY_SIZE * sizeof(float));

	if (array==NULL) {
		free(array);
		printf("Error allocating memory!\n"); //print an error message
		exit(1); //return with failure
	}

	for (i = 0; i < ARRAY_SIZE; i++) {
		array[i] = rand()%200;
	}

	return array;
}

void statistics() {
	float *array;
	int i;

	array = build();

	for (i = 0; i < ARRAY_SIZE; i++) {
		printf("Number %.2f\n", array[i]);
	}

	printf("\nAvg = %.2f\n", avg(array));
	printf("\nVariance = %.2f\n", var(array));
}
